<?php

namespace app\api\controller\permission;

use app\api\controller\common\ApiBase;
use think\Db;

class Sms extends ApiBase
{

    /**
     * 查看角色
     *
     * @return void
     */
    public function index()
    {
        if (request()->isPost()) {
            $page = $this->request->param('page');
            $page = max(1,intval($page));
            $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
            $exhibition_id =$this->request->param('exhibition_id');
            $param=" and w.is_delete=0 and w.type=0 ";
            if(!empty($exhibition_id)){
                $param.=" and w.exhibition_id=$exhibition_id ";
            }
            $keyword =input('keyword');
            if (!empty($keyword)) {
                $param.=" and w.title like '%$keyword%' ";
            }
            $sql2="select w.*,c.id as exhibition_id,c.title as exhibition_title";
            $sql2.=" from crm_email_sms as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
            $sql2.=" order by w.id desc ";
            $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
            $list=Db::query($sql2);
            $sql21=" select count(w.id) as count1 ";
            $sql21.=" from crm_email_sms as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
            $total1=Db::query($sql21);
            $totals=$total1[0]['count1']; //st
            return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$totals]]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 新增角色
     *
     * @return void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $data['sms_code'] = isset($post['sms_code']) && $post['sms_code'] != '' ? $post['sms_code'] : '';
            $data['sms_template'] = isset($post['sms_template']) && $post['sms_template'] != '' ? $post['sms_template'] : '';
            $data['exhibition_id'] = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            $data['sms_autograph'] = isset($post['sms_autograph']) && $post['sms_autograph'] != '' ? $post['sms_autograph'] : '';
            $data['sms_type'] = isset($post['sms_type']) && $post['sms_type'] != '' ? $post['sms_type'] : '';
            $data['sms_is_en'] = isset($post['sms_is_en']) && $post['sms_is_en'] != '' ? $post['sms_is_en'] : 1;
            if(!$data['sms_type']){
                return json(['code' => 0, 'msg' => '短信类型错误', 'data' => '']);
            }
            if(!$data['exhibition_id']){
                return json(['code' => 0, 'msg' => '展会信息错误', 'data' => '']);
            }
            $data['type'] = 0;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = db('email_sms')->insert($data);
            if(!$result){
                return json(['code' => 0, 'msg' => '操作失败', 'data' => '']);
            }
            return json(['code' => 1, 'msg' => '操作成功', 'data' => '']);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 编辑角色
     *
     * @return void
     */
    public function edit()
    {
        if (request()->isPost()) {
            if (request()->isPost()) {
                $post = input('post.');
                $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : '';
                $data['title'] = isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
                $data['sms_code'] = isset($post['sms_code']) && $post['sms_code'] != '' ? $post['sms_code'] : '';
                $data['sms_template'] = isset($post['sms_template']) && $post['sms_template'] != '' ? $post['sms_template'] : '';
                $data['exhibition_id'] = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
                $data['sms_autograph'] = isset($post['sms_autograph']) && $post['sms_autograph'] != '' ? $post['sms_autograph'] : '';
                $data['sms_type'] = isset($post['sms_type']) && $post['sms_type'] != '' ? $post['sms_type'] : '';
                $data['sms_is_en'] = isset($post['sms_is_en']) && $post['sms_is_en'] != '' ? $post['sms_is_en'] : 1;
                if(!$data['sms_type']){
                    return json(['code' => 0, 'msg' => '短信类型错误', 'data' => '']);
                }
                if(!$id){
                    return json(['code' => 0, 'msg' => '缺少关键字段', 'data' => '']);
                }
                if(!$data['exhibition_id']){
                    return json(['code' => 0, 'msg' => '展会信息错误', 'data' => '']);
                }
                $data['type'] = 0;
                $data['create_time'] = time();
                $data['update_time'] = time();
                $result = db('email_sms')->where(['id'=>$id])->update($data);
                if(!$result){
                    return json(['code' => 0, 'msg' => '操作失败', 'data' => '']);
                }
                return json(['code' => 1, 'msg' => '操作成功', 'data' => '']);
            } else {
                return json(array('code' => '0', 'msg' => '请求失败'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 删除角色
     *
     * @return void
     */
    public function del()
    {
        if (request()->isPost()) {
            $ids=$this->request->param('ids');
            if(empty($ids)){
                return json(['code'=>0,'msg'=>'观众数据有误','data'=>'']);
            }
            $person_id = explode(',',$ids);
            foreach ($person_id as &$v){
                $res=db('email_sms')->where(['id'=>$v])->update(['is_delete'=>1]);
            }
            unset($v);
            return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }
}
